package com.example.a517;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

public class MyService extends Service {
    class DownloadBinder extends Binder{
        public void startaDownoad(){
            Log.e("TAG", "startaDownoad: " );
        }

        public int getprogress (){
            Log.e("TAG", "getprogress: " );
            return 0;
        }
    }
    private DownloadBinder mBinder = new DownloadBinder();
    public MyService() {
    }
    public static final String CHANNEL_ID = "foregroundchannel";
    public static final int NOTIFICATION_ID = 1;
    @SuppressLint("ForegroundServiceType")
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TAG", "onCreate: " );
        Intent intent = new Intent(this,ServiceTest.class);
        PendingIntent pi = PendingIntent.getActivity(this,0,intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
        Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID).
                setContentTitle("this is a title").
                setContentText("this is a content text").
                setWhen(System.currentTimeMillis()).
                setSmallIcon(R.mipmap.aa)
                .setContentIntent(pi).build();
        startForeground(NOTIFICATION_ID,notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("TAG", "onStartCommand: " );
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("TAG", "onDestroy: ");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.e("TAG", "onBind: " );
        return mBinder;
    }
    private void createNotificationChannel(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID
                    ,"前台服务"
                    , NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}